<template>
	<view class="empowerBox">
		<u-popup v-model="show" mode="bottom">
			<view class="contentBox_e">
				<view>
					<image :src="appData.logo||'../../static/logo.png'" />
				</view>
				<view>
					<view class="">
						<text>
							是否授权【{{appData.name||"小程序名称"}}】
						</text>
					</view>
					<view>获取您的公开信息（昵称、头像、地区及性别）</view>
				</view>

				<view class="df-Jc-sa">
					<u-button type="primary" size="medium" @click="getUserInfo">确认授权</u-button>
					<u-button size="medium" @click="loginPopup">暂时不要</u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	//小程序配置
	import appDetail from "../../common/appConfig.json";
	import {
		mapMutations,
		mapState
	} from "vuex";
	export default {
		props: ['emShow'],
		data() {
			return {
				appData: null,
				show: false,
				destMemberId: null
			}
		},

		mounted() {
			this.appData = JSON.parse(appDetail)
			let userDetail = uni.getStorageSync("userDetail")
			//判断用户是否授权若未授权删除token
			if (!this.$noNull(userDetail)) {
				uni.removeStorageSync('token')
				this.login()
			} else {
				this.show = false
			}

		},
		computed: {
			...mapState(['userDetail'])
		},
		methods: {
			...mapMutations(['setUser']),
			getUserInfo(evt) {
				wx.getUserProfile({
					desc: '登录授权',
					success: res => {

						const userInfo = JSON.parse(res.rawData)

						let wxuser = {
							nickname: userInfo.nickName,
							avatarUrl: userInfo.avatarUrl,
							sex: userInfo.gender,
							country: userInfo.country,
							province: userInfo.province,
							city: userInfo.city
						}
						//提交用户信息
						this.$apis.updateUser(wxuser).then(resUpdata => {

							console.log(resUpdata, '已更新')
							//保存用户信息
							this.setUser(wxuser)
							uni.setStorageSync("userDetail", wxuser)
							this.$toast("授权成功", 1)
							uni.$emit("updateUser")
							this.show = false
						})
						this.binding()
					}
				})

			},
			binding() {
				//是否需要绑定
				uni.getStorage({
					key: 'destMemberId',
					success: (res) => {
						this.destMemberId = res.data
						//关系绑定
						if (this.destMemberId !== null) {

							this.$apis.bind({

								scene: this.destMemberId,

							}).then(res => {

								console.log('已经绑定分销关系')

								uni.removeStorageSync('destMemberId')
							})
						}
					}
				})
			},
			login() {
				uni.getStorage({
					key: "token",
					success: () => {
						uni.getStorage({
							key: 'userDetail',
							success: (res) => {
								this.show = false
								this.binding()

							},
							fail: () => {
								this.show = true
							}
						})
					},
					fail: () => {
						uni.login({
							timeout: 4000,
							success: loginData => {
								this.$apis.code2session({
									code: loginData.code
								}).then(tokenData => {
									uni.setStorageSync('token', tokenData.obj.jwt.token)
									this.login()
								})
							}
						})
					}
				})

			},
			loginPopup() {
				this.show = false
				setTimeout(() => {
					this.$emit('empowerFu', 'loginPopup')
				}, 500)
			},

		}
	}
</script>

<style scoped lang="scss">
	.empowerBox {
		.contentBox_e {
			padding: 50rpx 0 70rpx 0;
			text-align: center;

			image {
				@include boxs(200rpx, 200rpx, 200)
			}

			>view:nth-child(2) {
				text {
					color: #2979FF;
				}

				margin: 20rpx 0;
				@include fz(25, #000, 50);
			}

			>view:nth-child(3) {
				margin: 0 auto;
			}
		}
	}
</style>
